<?php

namespace App\Services\Core\Curd;

use App\Services\Core\Traits\Response;
use Illuminate\Support\Facades\DB;

class CurdServicesProvider
{

    use Response, SaveService, DeleteService, SelectService;

    public $model;

    public $modelClass;

    public array $options = [];

    public $ids = [];

    public array $hidden = [];

    public $result;

    private $inst;

    public function __construct($modelClass = '')
    {
        if ($modelClass) {
            $this->modelClass = $modelClass;
            $this->model      = new $modelClass;
        }

        return $this;
    }

    public function initCurd(string $modelClass)
    {
        return self::__construct($modelClass);
    }

    public function getModelConst(string $constName)
    {
        return constant($this->modelClass . '::' . strtoupper($constName));
    }

    public function getModelAttrVal(string $constName, string $field)
    {
        return $this->getModelConst($constName)[$field]['value'];
    }

    public function beginTransaction()
    {
        DB::beginTransaction();
    }

    public function rollback(string $msg = '')
    {
        DB::rollBack();
        $this->fail(msg:$msg);
        return false;
    }

    public function commit()
    {
        DB::commit();
        return true;
    }

}
